<?php
namespace app\index\controller;
//error_reporting('0');
class indexController extends \app\base\controller\BaseController
{
    public function index(){
if(!file_exists(CONFIG_PATH.'install.lock')){
	$this->redirect("index.php?r=install");
}

    	//获取分类
        $this->list = obj("index/global", "controller")->malltype("y", "0", "category", "null");
		$sql = "select * from `{pre}category` where `mallType`=0 and ID in (select min(ID) from `{pre}category` group by `mallName`)";
		$this->mall= obj('api/ApiData')->thisquery($sql);


        //半个小时热门
        $where_30m[]="`mallType` =  '0' and `date`> DATE_SUB(NOW(),INTERVAL  30 MINUTE)";
        $minute=obj("api/Apidata")->Data_Select("goods",$where_30m,"`date` DESC LIMIT 0 , 9");
        $this->minute=$minute;


        //热门点击
        $where_hot[]="1";
        $hot=obj("api/Apidata")->Data_Select("goods",$where_hot,"`id` DESC LIMIT 0 , 10");
        $this->hot=$hot;

        //7天收录
        $tenitems[]=" `mallType` =  '0' and  date_sub(curdate(), INTERVAL 7 DAY) <= date(`date`) ";
        $this->tenitems=obj("api/ApiData")->Data_Count("goods",$tenitems);

        //今日发布+预计
        $date=date("Y-m-d",time());
        $todaysenddata[]=" `mallType` =  '0' and  `date` LIKE  '%{$date}%'";
        $this->todaysenddata=obj("api/ApiData")->Data_Count("goods",$todaysenddata);
        $this->country="国内";

        $where[]=" 1";
        $baseurl=url($route='index/index/index', $params=array());
        if($this->arg("list")){
			
			$wheres1[]="`id` ='{$this->arg("list")}'";
	 $cf1=obj("api/Apidata")->Data_Select("category",$wheres1);
	 
		//$lists=urldecode($this->arg("list"));
        $where[]="`mallType` =  '0' AND  `1stCategoryName` ='{$cf1['1stCategoryName']}'";
        $baseurl=url($route='index/index/index/list=<list>', $params=array("list"=>$this->arg("list")));

        }

        if($this->arg("mall")){
			
			$wheres2[]="`id` ='{$this->arg("mall")}'";
	     $cf2=obj("api/Apidata")->Data_Select("category",$wheres2);
	 
         $where[]="`mallType` =  '0' AND  `mallName` ='{$cf2['mallName']}'";
         $baseurl=url($route='index/index/index/mall=<mall>', $params=array("mall"=>$this->arg("mall")));

        }

        if($this->arg("list") && $this->arg("mall")){
	    
		$wheres3[]="`id` ='{$this->arg("list")}'";
	 $cf3=obj("api/Apidata")->Data_Select("category",$wheres3);
	 
         $where[]="`mallType` =  '0' AND  `mallName` =  '{$cf3['mallName']}' AND `1stCategoryName` =  '{$cf3['1stCategoryName']}'";
         $baseurl=url($route='index/index/index/list=<list>/mall=<mall>', $params=array("list"=>$this->arg("list"),"mall"=>$this->arg("mall")));
        }

        $Page = obj('api/ApiData')->PageIndex("50", "goods", $where, "`date` DESC", $baseurl);
        $this->Page = $Page;

        //max id
        $maxidsql="SELECT MAX( id ) as maxid FROM  `{pre}goods`";
        $maxid=obj("api/ApiData")->thisquery($maxidsql);
        $this->maxid=$maxid['0']['maxid'];


        


    	$this->display();
    }	


    public  function welcomecookie(){

    	setcookie("welcome", "zhicms", time()+6600);
    }

    public function log(){

        echo "callback({ data:\"ok\"})";
    }

    public function checknum(){

       $maxid=$this->arg("maxid");
       if(!is_numeric($maxid)){
        exit("erorr");
       }

        $maxidsql="SELECT MAX( id ) as maxid FROM  `{pre}goods`";
        $mysqlmaxid=obj("api/ApiData")->thisquery($maxidsql);
        $newmaxid=$mysqlmaxid['0']['maxid']-$maxid;
        echo $newmaxid;


    }


    //预估数据计算
    public function estimate($type){

        if($type=="cn"){
            $sql="`mallType` = '0' and";
        }elseif($type=="us"){

           $sql="`mallType` = '1' and ";
        }

        //昨日此时发布了多少数据
        $lastdaysql="SELECT count(*) as lastdaycount FROM {pre}goods WHERE {$sql} TO_DAYS( NOW( ) ) - TO_DAYS( date) <= 1";
        $lastret=obj("api/ApiData")->thisquery($lastdaysql);

        //今日数据
        $todaysql="select count(*) as todaycount from {pre}goods where {$sql} to_days(date) = to_days(now())";
        $todayret=obj("api/ApiData")->thisquery($todaysql);
        $newcount=$lastret['0']['lastdaycount']+$todayret['0']['todaycount'];

        if($newcount<=0){
            $newcount="0";
            $bfb="0";
        }else{
            $newcount=$newcount+$todaycount;
            $bfb=round($todayret['0']['todaycount']/$lastret['0']['lastdaycount']*100);
        }

        
        return array("bfb"=>$bfb,"count"=>$newcount);

    }

  }